﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{   
    /// <summary>
    /// RESPONSABILITY: - employee repository
    ///                 - create new employee
    ///                 - add employee to the list of employees
    ///                 - delete an employee
    ///                 - change the employee code
    ///                 - change the employee name 
    /// </summary>
    public class ChangeEmployee
    {
        private List<Employee> employees = new List<Employee>();
        public IEnumerable<Employee> GetEmployees(List<string> codes = null)
        {
            if (codes == null)
            {
                return employees;
            }

            var result = employees.Where(c => codes.Contains(c.EmployeeCode));

            return result;
        }

        public List<Role> roles=new List<Role>();
        
        public Employee CreateEmployee(string employeeName, string employeeCode, DateTime hireDate, double baseWage)
        {
            if (string.IsNullOrEmpty(employeeCode))
            {
                throw new ArgumentException("The employee cod cannot be null", employeeCode);
            }

            Employee employee = new Employee(employeeName, employeeCode, hireDate, baseWage);
            this.AddEmployee(employee);
            return employee;        
        }

        public void AddEmployee(Employee employee)
        {
            if (CheckEmployeeIDExistance(employee.EmployeeId))
            {
                return;
            }

            if (CheckEmployeeCodeExistance(employee.EmployeeCode))
            {
                throw new Exception("A employee with the code " + employee.EmployeeCode + " already exists");
            }

            employees.Add(employee);
        }

        private bool CheckEmployeeCodeExistance(string code)
        {
            var _employee = employees.FirstOrDefault(e => e.EmployeeCode == code);

            var isFound = _employee != null;

            return isFound;
        }

        private bool CheckEmployeeIDExistance(int id)
        {
            var _employee = employees.FirstOrDefault(e => e.EmployeeId == id);

            var isFound = _employee != null;

            return isFound;
        }

        public void DeleteEmployee(Employee employee,string employeeCode)
        {
            Employee removeEmployee = employees.FirstOrDefault(e => e.EmployeeId == employee.EmployeeId);
            if (removeEmployee != null)
            {
                employees.Remove(employee);
            }    
        }

        public void ChangeEmployeeCode(Employee employee,string newEmployeeCode)
        {
            Employee foundEmployee = employees.FirstOrDefault(e => e.EmployeeId == employee.EmployeeId);
            if (foundEmployee != null)
                employee.EmployeeCode = newEmployeeCode;
        }


        public void ChangeEmployeeName(Employee employee,string newEmployeeName)
        {
            
            Employee foundEmployee = employees.FirstOrDefault(e=>e.EmployeeId==employee.EmployeeId);
            if (foundEmployee != null)
            {
               employee.EmployeeName = newEmployeeName;
            }            
        }       
    }
}